package jmine.tec.services.utils.replace.hssf;

import java.util.ArrayList;
import java.util.List;

import jmine.tec.services.utils.replace.api.ServiceBlock;
import jmine.tec.services.utils.replace.api.ServiceHeader;
import jmine.tec.services.utils.replace.api.ServiceRow;

import org.apache.poi.hssf.usermodel.HSSFRow;

import bancosys.tec.services.input.driver.xls.XLSUtils;

public class HSSFServiceBlock implements ServiceBlock {

    private final HSSFServiceHeader header;

    private final List<HSSFServiceRow> services;

    public HSSFServiceBlock(String sheetName, XLSUtils xlsUtils, List<HSSFRow> rows) {
        this.header = new HSSFServiceHeader(rows.get(0));
        this.services = new ArrayList<HSSFServiceRow>(rows.size() - 1);
        for (HSSFRow row : rows.subList(1, rows.size())) {
            this.services.add(new HSSFServiceRow(sheetName, this.header, row, xlsUtils));
        }
        // Matcher actionMatcher = Pattern.compile("(?:#)?(\\w+)").matcher(this.services.get(0).getAction());
        // Matcher nameMatcher = Pattern.compile("(?:#)?(\\w+)").matcher(this.services.get(0).getName());
        // if (!actionMatcher.matches()) {
        // throw new RuntimeException("Não é possível fazer match da action do serviço!");
        // }
        // if (!nameMatcher.matches()) {
        // throw new RuntimeException("Não é possível fazer match do nome do serviço!");
        // }
        // Pattern actionPattern = Pattern.compile("(?:#)" + actionMatcher.group(1));
        // Pattern namePattern = Pattern.compile("(?:#)" + nameMatcher.group(1));
        // for (ServiceRow row : this.services) {
        // if (!actionPattern.matcher(row.getAction()).matches() || !namePattern.matcher(row.getName()).matches()) {
        // throw new RuntimeException("Block de serviços com múltiplos tipos de serviços encontrado.");
        // }
        // }
    }

    /*
     * (non-Javadoc)
     * @see jmine.biz.invest.operacional.liquidacao.services.replace.ServiceBlock#getHeader()
     */
    public ServiceHeader getHeader() {
        return this.header;
    }

    /*
     * (non-Javadoc)
     * @see jmine.biz.invest.operacional.liquidacao.services.replace.ServiceBlock#getServices()
     */
    public List<? extends ServiceRow> getServices() {
        return this.services;
    }

    @Override
    public String toString() {
        return String.format("Bloco de %s serviços de %s:%s", this.services.size(), this.services.get(0).getAction(), this.services.get(0)
                .getName());
    }

    public void addParameter(String parameter) {
        this.header.addParameter(parameter);
        for (HSSFServiceRow row : this.services) {
            row.addParameter(parameter);
        }
    }

    public void removeParameter(String parameter) {
        for (HSSFServiceRow row : this.services) {
            row.removeParameter(parameter);
        }
        this.header.removeParameter(parameter);
    }
}
